# -*- coding: utf-8 -*-
# 多线程

import time,threading,multiprocessing

# 新线程执行的代码
def loop():
	print('thread %s is running...' % threading.current_thread().name)
	n = 0
	while n < 5:
		n = n + 1
		print('thread %s >>> %s' % (threading.current_thread().name,n))
		time.sleep(1)
	print('thread %s ended.' % threading.current_thread().name)

print('thread %s is running...' % threading.current_thread().name)
t = threading.Thread(target=loop,name='LoopThread')
t.start()
t.join()
print('thread %s ended.' % threading.current_thread().name)



# lock
balance = 0
lock = threading.Lock()

def change_it(n):
	global balance
	balance = balance + n
	balance = balance - n
def run_thread(n):
	for i in range(100000):
		# 先要获取锁：
		lock.acquire()
		try:
			change_it(n)
		finally:
			lock.release()
t1 = threading.Thread(target=run_thread,args=(5,))
t2 = threading.Thread(target=run_thread,args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)


# python中 有一个gli全局锁，保证所有线程只在一个cpu上运行
# def loop():
# 	x = 0
# 	while True:
# 		x = x ^ 1

# for i in range(multiprocessing.cpu_count()):
# 	t = threading.Thread(target=loop)
# 	t.start()



##### ThreadLocal
local_school = threading.local()

def process_student():
	# 获取当前线程关联的student
	std = local_school.student
	print('Hello, %s (in %s)' % (std, threading.current_thread().name))

def process_thread(name):
	# 绑定ThreadLocal的student
	local_school.student = name
	process_student()

t1 = threading.Thread(target=process_thread,args=('Alice',),name='Thread-A')
t2 = threading.Thread(target=process_thread,args=('Bob',),name='Thread-B')

t1.start()
t2.start()
t1.join()
t2.join()


# 一个ThreadLocal变量虽然是全局变量，
# 但每个线程都只能读写自己线程的独立副本，互不干扰。
# ThreadLocal解决了参数在一个线程中各个函数之间互相传递的问题